﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Public Class FormLxCk
    Dim objPgdMes As New clsPgdMes
    Dim objPgdMesWorkReport As New clsPgdMesWorkReport
    Dim httpHelper As New HttpHelper
    Dim objLXCK As New clsLXCK
    Private dtLXCK As DataTable
    Dim warehousecode As String
    Public usr As String
    Dim txt As TextBox
    Private Sub FormLxCk_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        dgv.Font = New Font("UTF-8", 14)
        warehousecode = GetIniFile("WAREHOUSE", "warehouse_code", "", g_szInitFileName)
        Dim startDate As String = Date.Now.ToShortDateString()
        txtStartDate.Text = startDate
        Dim endDate As String = Date.Now.ToShortDateString()
        txtEndDate.Text = endDate
        setDgvData()
    End Sub
    Private Sub setDgvData()
        If String.IsNullOrEmpty(txtStartDate.Text.Trim()) = False And String.IsNullOrEmpty(txtEndDate.Text.Trim()) = False Then
            dtLXCK = objLXCK.getList(txtProdId.Text.Trim(), cmbWHNo.Text,
                                    txtStartDate.Text.Trim(),
                                    txtEndDate.Text.Trim())
            dtLXCK.Columns.Add("status_name")
            dtLXCK.Columns.Add("lengthOfTime")
            Dim dtime As DateTime = getServerDateTime()
            If dtLXCK.Rows.Count > 0 Then
                For Each row As DataRow In dtLXCK.Rows
                    If row("status") = "0" Then
                        row("status_name") = "在库"
                    Else
                        row("status_name") = "已出库"
                    End If
                    If IsDBNull(row("out_date")) Then
                        row("lengthOfTime") = (dtime - DateTime.Parse(row("into_date"))).ToString()
                    Else
                        row("lengthOfTime") = (DateTime.Parse(row("out_date")) - DateTime.Parse(row("into_date"))).ToString()
                    End If
                Next
            End If
            Dim dv As DataView = dtLXCK.DefaultView
            dv.Sort = "lengthOfTime desc"
            Dim dtCopy As DataTable = dv.ToTable()
            dgv.AutoGenerateColumns = False
            dgv.DataSource = dtCopy
            lblCount.Text = dgv.Rows.Count
        Else
            MsgBox("请选择开始时间和结束时间", MsgBoxStyle.Information, "提示")
        End If

    End Sub
    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        setDgvData()
    End Sub

    Private Sub btnRk_Click(sender As Object, e As EventArgs) Handles btnRk.Click
        Dim formRk As New FormRk()
        formRk.usr = usr
        formRk.ShowDialog(Me)
    End Sub

    Private Sub btnDELIVER_Click(sender As Object, e As EventArgs) Handles btnDELIVER.Click
        Dim formDeliver As New FormDeliver()
        formDeliver.usr = usr
        formDeliver.ShowDialog(Me)
    End Sub

    Private Sub btnRkMes_Click(sender As Object, e As EventArgs) Handles btnRkMes.Click
        If dgv.SelectedRows.Count > 0 Then
            Dim dtPgdMes As DataTable = objPgdMes.getListBySysId(dgv.SelectedRows(0).Cells("b_pgd_mes_sys_id").Value.ToString())
            Dim rnt_mes_rk As String = mesRk("1", warehousecode,
                                                        dtPgdMes.Rows(0)("type_value").ToString(),
                                                        dtPgdMes.Rows(0)("mes_project_code").ToString())
            Dim jo_rk As JObject = JsonConvert.DeserializeObject(rnt_mes_rk)
            Dim code_rk As String = jo_rk("code").ToString() '输出0
            Dim msg_rk As String = jo_rk("msg").ToString() '输出操作成功
            objLXCK.update_IntoCK_MES(txtProdId.Text.Trim(), "0", code_rk)
            If code_rk.Equals("0") Then
                MsgBox("MES入库成功", vbYes, "提示")
            Else
                MsgBox("MES入库失败：" & msg_rk, vbYes, "提示")
            End If
        Else
            MsgBox("请选择库存支座", vbYes, "提示")
        End If

    End Sub

    Private Function mesRk(oper_num As String, warehousecode As String, productorname As String, b_gtPgdMesProjectCode As String) As String
        Dim params As String = "{"
        params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
        params &= """rkcount"":""" & Double.Parse(oper_num) & """," '入库数量
        params &= """warehousecode"":""" & warehousecode & """," '入库仓库编码
        params &= """productorname"":""" & productorname & """," '产品编码
        params &= """procedurename"":""" & "力学" & """," '执行工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """departmentid"":""TS0901""" '部门id
        params &= "}"
        Return httpHelper.HttpPostWebService("productFinishedToWarehouse", params)
    End Function

    Private Sub btnBgMes_Click(sender As Object, e As EventArgs) Handles btnBgMes.Click
        If dgv.SelectedRows.Count > 0 Then
            Dim prod_id As String = dgv.SelectedRows(0).Cells("prod_id").Value.ToString()
            Dim PgdMesSysId As String = dgv.SelectedRows(0).Cells("b_pgd_mes_sys_id").Value.ToString()
            'objPgdMesWorkReport.insert("ok", PgdMesSysId, "1", "力学", prod_id, "4", "0")
            Dim dtPgdMes As DataTable = objPgdMes.getListBySysId(PgdMesSysId)
            Dim b_PgdMesProjectCode As String = dtPgdMes.Rows(0)("mes_project_code").ToString()
            Dim b_PgdMes_gx As String = dtPgdMes.Rows(0)("type_value").ToString()
            Dim rtn_mes_bg As String = mesbg("1", b_PgdMes_gx, b_PgdMesProjectCode)
            Dim jo As JObject = JsonConvert.DeserializeObject(rtn_mes_bg)
            Dim code As String = jo("code").ToString() '输出0
            Dim msg As String = jo("msg").ToString() '输出操作成功
            'objPgdMesWorkReport.insert("ok", PgdMesSysId, "1", usr, prod_id, "4", code)
            If code.Equals("0") Then
                MsgBox("保存成功，MES报工成功", vbYes, "提示")
            Else
                MsgBox("保存成功，MES报工失败：" & msg, vbYes, "提示")
            End If
        End If
    End Sub

    Private Function mesbg(oper_num As String, productorname As String, b_gtPgdMesProjectCode As String) As String
        Dim params As String = "{"
        params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
        params &= """qualifiednumber"":""" & Double.Parse(oper_num) & """," '报工的合格数
        params &= """unqualifiednumber"":""" & 0.00 & """," '报工的不合格数
        params &= """productorname"":""" & productorname & """," '报工产品编码
        params &= """procedurename"":""" & "力学" & """," '报工工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """unqualifiedReason"":""" & "" & """" '不合格原因
        params &= "}"
        Return httpHelper.HttpPostWebService("dispatchingOrderBG", params)
    End Function

    Private Sub txt_MouseClick(sender As Object, e As MouseEventArgs) Handles txtStartDate.MouseClick, txtEndDate.MouseClick
        txt = CType(sender, TextBox)
        DateNavigator1.Left = txt.Left
        DateNavigator1.Top = txt.Location.Y + txt.Height
        DateNavigator1.Visible = True
    End Sub

    Private Sub DateNavigator1_DateTimeChanged(sender As Object, e As EventArgs) Handles DateNavigator1.DateTimeChanged
        txt.Text = DateNavigator1.DateTime.ToShortDateString()
        DateNavigator1.Visible = False
    End Sub

End Class